package com.ithema.service.impl;

import com.ithema.entity.Result;
import com.ithema.mapper.UserMapper;
import com.ithema.service.UserService;
import com.ithema.utils.JwtUtil;
import com.ithema.utils.Md5Util;
import com.ithema.entity.User;
import com.ithema.utils.ThreadLocalUtil;
import io.netty.util.Timeout;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
@Autowired
    private StringRedisTemplate redisTemplate;
    @Override
    public int register(User user) {
        //判断用户名是否存在
        if (user.getUsername() == null) {
            return 1;
        } else {
            //用户是否已经存在
            if (userMapper.findByUserName(user.getUsername()) != null) {
                return 1;
            } else {
                //注册，新增用户
                user.setPassword(Md5Util.getMD5String(user.getPassword()));
                userMapper.register(user);
                return 0;
            }
        }

    }

    @Override
    public Result login(String username, String password) {
        User user = userMapper.findByUserName(username);
        //判断密码是否正确
        if (user != null && user.getPassword().equals(Md5Util.getMD5String(password))) {
            //生成token，存储用户id
            Map<String, Object> claims = new HashMap<>();
            claims.put("userId", user.getId());
            claims.put("username", username);
            String token = JwtUtil.getToken(claims);
            //token存入redis
            ValueOperations<String, String> operations = redisTemplate.opsForValue();
            operations.set("token",token, 1, TimeUnit.HOURS);
            //密码正确
            return Result.success(token);
        }
        return Result.error("登录失败，账号或密码错误");
    }

    @Override
    public User findByUserName(String user) {
        return userMapper.findByUserName(user);
    }

    @Override
    public int update(User user) {
        Map<String, Object> map = ThreadLocalUtil.get();
        int id = (int) map.get("userId");
        user.setId(id);
        userMapper.update(user);
        return 1;
    }

    @Override
    public int updateAvatar(String avatar) {
        Map<String, Object> map = ThreadLocalUtil.get();
        int id = (int) map.get("userId");
        userMapper.updateAvatar(avatar, id);
        return 1;
    }

    @Override
    public int updatePassword(String oldPassword, String newPassword) {
        //获取用户信息
        Map<String, Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("username");
        //判断用户旧密码是否正确
        User user = userMapper.findByUserName(username);
        if (user.getPassword().equals(Md5Util.getMD5String(oldPassword))) {
            //旧密码正确 修改密码
            userMapper.updatePassword(Md5Util.getMD5String(newPassword), username);
            //清空redis缓存
            redisTemplate.delete("token");
            return 1;
        }
        return 0;
    }

}
